package com.zy.garbage.controller;

import com.zy.garbage.enums.PermsEnum;
import com.zy.garbage.enums.ResultEnum;
import com.zy.garbage.enums.UserStatusEnum;
import com.zy.garbage.exception.CommonException;
import com.zy.garbage.form.CreateAccountForm;
import com.zy.garbage.service.ExamService;
import com.zy.garbage.service.QuestionService;
import com.zy.garbage.util.ResultUtil;
import com.zy.garbage.vo.ExamVO;
import com.zy.garbage.vo.QuestionListVO;
import com.zy.garbage.vo.ResultVO;
import com.zy.garbage.vo.UserExamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/exam")
public class ExamController extends BaseController {
    @Autowired
    private ExamService examService;
    @Autowired
    private QuestionService questionService;

    @PostMapping("/create/{userId}")
    public ResultVO create(@PathVariable("userId") Integer userId, HttpServletResponse response) {
        try {
            Integer examId = examService.save(userId);
            QuestionListVO questionListVO = questionService.save(examId);
            return ResultUtil.success(questionListVO);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("/showByUser/{userId}")
    public ResultVO showByUser(@PathVariable("userId") Integer userId, HttpServletResponse response) {
        try {
            List<ExamVO> examVOList = examService.findByUserId(userId);
            return ResultUtil.success(examVOList);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("/showAll")
    public ResultVO showAll() {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                List<ExamVO> examVOList = examService.findAll();
                return ResultUtil.success(examVOList);
            } catch (CommonException e) {
                throw new CommonException(500, "查询失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }
}
